package com.android.ailaoshan.camera;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.widget.Toast;

import com.android.ailaoshan.index.MainActivity;
import com.android.ailaoshan.tools.BaseApplication;

import java.io.File;
import java.io.IOException;

public class StartCamera {

    private Context mContext;
    private MainActivity mainActivity;

    private BaseApplication baseApplication;

    private final String PIC_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Picture" +File.separator;

    public StartCamera(Context mContext, MainActivity mainActivity) {
        this.mContext = mContext;
        this.mainActivity = mainActivity;

        this.baseApplication = BaseApplication.getInstance();

        openCamera();
    }

    /**
     * 判断是否有摄像机
     */
    private boolean isExistCamera(){
        PackageManager manager = mContext.getPackageManager();
        return (manager.hasSystemFeature(PackageManager.FEATURE_CAMERA) ||
                manager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT));
    }

    /**
     * 创建文件
     */
    private File createImageFile(){
        // 检查存储文件夹是否存在，不存在则创建
        File folder = new File(PIC_PATH);

        boolean mkDirSuccess = true;
        if(!folder.exists()){
            mkDirSuccess = folder.mkdirs();
        }

        // 创建图片文件
        File imageFile = null;
        if(mkDirSuccess){
            String imageFileName = System.currentTimeMillis()+"";

            try{
                imageFile = File.createTempFile(imageFileName,".jpg",folder);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return imageFile;
    }

    /**
     * 跳转摄像机
     */
    private void openCamera(){

        // 存在照相机
        if(isExistCamera()){
            File imageFile = createImageFile();

            // 图片文件创建成功
            if(imageFile != null){
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri tempPath;

                //手机版本在安卓7及以上，使用FileProvider获得Uri
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    tempPath = FileProvider.getUriForFile(mContext,mContext.getPackageName()+".provider",imageFile);
                }else{
                    tempPath = Uri.fromFile(imageFile);
                }
                // 照片存储路径
                intent.putExtra(MediaStore.EXTRA_OUTPUT,tempPath);

                // 存储路径缓存
                baseApplication.setImageFile(tempPath.getPath());

                // Activity跳转
                mainActivity.startActivityForResult(intent,201);

            }else{
                Toast.makeText(mContext,"创建照片文件失败，请检查你是否有存储权限",
                        Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(mContext,"在这个设备上没有找到摄像机",Toast.LENGTH_LONG).show();
        }
    }
}
